//
//  FindPassViewC.m
//  WHSport
//
//  Created by ccjoy-jimneylee on 15/5/11.
//  Copyright (c) 2015年 jimneylee. All rights reserved.
//

#import "FindPassViewC.h"
#import "Util.h"
#import "UserModel.h"
#import <MBProgressHUD.h>

@interface FindPassViewC ()

@end

@implementation FindPassViewC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title =@"找回密码";
    _passField.secureTextEntry = YES;
    _compField.secureTextEntry = YES;
    UIBarButtonItem *leftBtn = [[UIBarButtonItem alloc]
                                initWithImage:[UIImage imageNamed:@"01"]
                                style:UIBarButtonItemStylePlain
                                target:self action:@selector(dismiss:)];
    self.navigationItem.leftBarButtonItem =leftBtn;
}

- (void)dismiss:(id)sender{
    
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField;
{
    [textField resignFirstResponder];
    return YES;
}

- (void)textFieldDidEndEditing:(UITextField *)textField{
    
    if (textField == _nameField) {
        [self.view endEditing:YES];
        NSString *name = _nameField.text;
        [Util changeNSString:name];
        if (name.length != 11) {
            UIAlertView *alertvc = [[UIAlertView alloc] initWithTitle:@"提示"
                                                              message:@"请正确填写11位手机号码"
                                                             delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alertvc show];
            return ;
        }
        
        [[UserModel sharedInstance] checkAccountIsExist:name successTele:^(BOOL successed, unsigned int value) {
                if (successed) {
                    MBProgressHUD *HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
                    HUD.labelText = @"用户名未注册";
                    [HUD hide:YES afterDelay:1.5f];
            }
//            else{
//                if (value == 0) {
//                    HUD.labelText = @"用户名已存在请重新填写";
//                    [HUD hide:YES afterDelay:1.5f];
//                }
//            }
        }];
        
    }
    
    if (textField == _codeField) {
        [self.view endEditing:YES];
        NSString *code = _codeField.text;
        NSString *name = _nameField.text;
        [Util changeNSString:code];
        [[UserModel sharedInstance] checkSmsCode:name smscode:code
                               successTeleNumber:^(BOOL successed, unsigned int value) {
                                   MBProgressHUD *HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
                                   if (successed) {
                                       HUD.labelText = @"验证码输入正确";
                                       [HUD hide:YES afterDelay:1.5f];
                                   }
                                   else{
                                       if (value == 1) {
                                           HUD.labelText = @"验证码输入错误请重新填写";
                                           [HUD hide:YES afterDelay:1.5f];
                                       }
                                   }
                               }];
    }
}

- (IBAction)codeBtn:(id)sender{
    
    NSString *tele = _nameField.text;
    [Util changeNSString:tele];
    [[UserModel sharedInstance] getSmsCode:tele
                            successTeleNum:^(BOOL successed, unsigned int value) {
                            }];
    _codeBtn.enabled = NO;
}

- (IBAction)comBtn:(id)sender{
    
    [self.view endEditing:YES];
    NSString *cphone = _nameField.text;
    NSString *cpass = _passField.text;
    NSString *ccom = _compField.text;
    [Util changeNSString:cphone];
    [Util changeNSString:cpass];
    [Util changeNSString:ccom];
    if ([cpass isEqualToString:ccom]) {
        [[UserModel sharedInstance] findPasswordWithPhone:cphone password:cpass
        successFind:^(BOOL successed, unsigned int value) {
            if (successed) {
                MBProgressHUD *HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
                HUD.labelText = @"修改密码成功";
                [HUD hide:YES afterDelay:1.5f];
                [self dismissViewControllerAnimated:YES completion:^{
                }];
            }
    }];
    }
}


@end
